﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Attendences.Framework.Entity;
using Wicresoft.Framework.Authentication;
using Wicresoft.Common;

public partial class Page_EditOtherUser : BasePage
{
    public Guid UserID
    {
        get
        {
            if (Request.QueryString["ID"] != null)
                return new Guid(Request.QueryString["ID"]);
            return Guid.Empty;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (UserID != Guid.Empty)
            {
                OtherUser user = new OtherUser(UserID);
                if (user.Load())
                {
                    tbMobile.Text = user.Mobile;
                    tbName.Text = user.DisplayName;
                    tbPhone.Text = user.Phone;
                    tbAddress.Text = user.Address;
                    tbEmail.Text = user.Email;
                    tdAdd.Visible = false;
                    tdSave.Visible = true;
                }
                else
                {
                    tdAdd.Visible = true;
                    tdSave.Visible = false;
                }
            }
        }
    }
    protected void SaveUser(object sender, EventArgs e)
    {
        LinkButton lbtn = sender as LinkButton;
        if (lbtn.CommandName == "Add")
        {
            OtherUser user = new OtherUser();
            user.ID = Guid.NewGuid();
            user.Mobile = tbMobile.Text;
            user.Name = tbName.Text;
            user.Phone = tbPhone.Text;
            user.Address = tbAddress.Text;
            user.CreateTime = DateTime.Now;
            user.Creator = CurrentUser.Name;
            user.CreatorDisplay = CurrentUser.DisplayName;
            user.DisplayName = tbName.Text;
            user.Email = tbEmail.Text;
            user.Add();

            DataAccess.Update(typeof(OtherUser), "Pwd='" + Helper.MD5Hash(tbPwd.Text.Trim() == string.Empty ? "111111" : tbPwd.Text.Trim()) + "'", "ID='" + user.ID + "'");
            
            ClientScript.RegisterStartupScript(GetType(), "save", "alert('添加成功');window.opener.refresh();window.close();", true);
        }
        if (lbtn.CommandName == "Save")
        {
            OtherUser user = new OtherUser(UserID);
            user.Load();
            user.Mobile = tbMobile.Text;
            user.Name = tbName.Text;
            user.Phone = tbPhone.Text;
            user.Address = tbAddress.Text;
            user.CreateTime = DateTime.Now;
            user.Creator = CurrentUser.Name;
            user.CreatorDisplay = CurrentUser.DisplayName;
            user.DisplayName = tbName.Text;
            user.Email = tbEmail.Text;
            user.UpdateByPrimaryKey();
            DataAccess.Update(typeof(OtherUser), "Pwd='" + Helper.MD5Hash(tbPwd.Text.Trim() == string.Empty ? "111111" : tbPwd.Text.Trim()) + "'", "ID='" + user.ID + "'");
            
            ClientScript.RegisterStartupScript(GetType(), "save", "alert('保存成功');window.opener.refreshParent();window.close();", true);
        }
    }
}